package com.changgou.user.listener;

import com.alibaba.fastjson.JSON;
import com.changgou.order.pojo.Task;
import com.changgou.user.config.RabbitMQConfig;
import com.changgou.user.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;


@Component
public class AddPointListener {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserService userService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RabbitListener(queues = RabbitMQConfig.CG_BUYING_ADDPOINT)
    public void receiveAddPointMessage(String message) {
        System.out.println("用户服务接收到了任务消息");

        //转换消息
        Task task = JSON.parseObject(message, Task.class);
        //如果消息队列中存在相同任务，则返回 避免消息的重复消费
        if (task == null || StringUtils.isEmpty(task.getRequestBody())) {
            return;
        }

        //判断redis中当前的任务是否存在
        Object value = redisTemplate.boundValueOps(task.getId()).get(); //如果redis中存在相同的任务id task id 则返回，避免消息的重复消费
        if (value != null) {
            return;
        }

        //更新用户积分
        int result = userService.updateUserPoint(task);
        //如果返回结果为0 则代表用户增加失败 ，直接返回  1代表增加成功，继续向下执行
        if (result == 0) {
            return;
        }

        //给订单通知服务发送消费成功消息
        rabbitTemplate.convertAndSend(RabbitMQConfig.EX_BUYING_ADDPOINTUSER, RabbitMQConfig.CG_BUYING_FINISHADDPOINT_KEY, JSON.toJSONString(task));
        System.out.println("用户服务向完成添加积分队列发送了一条消息");
    }
}
